<?php
$dt = new DataTable(constant('DB_TABLE_WORKING_IMG'));
$GLOBALS['GV_IMAGE_PATH'] = 'working/';
$act = isset($_REQUEST['act'])?$_REQUEST['act']:'';
if ( $act == 'save') {
	$request_data = $_REQUEST;
	if ( strlen($request_data['added_img']) > 0) {
		$added_img_list = explode(',', $request_data['added_img']);
		foreach($added_img_list as $tmp_img_name)
		{
			$GLOBALS['APP_INSTANCE']->saveUploadedFile($tmp_img_name, $GLOBALS['GV_IMAGE_PATH'] );
			$working_image_data = array(
					'working_id'=>$request_data['working_id'],
					'path'=>$tmp_img_name
			);
			$dt->save($working_image_data);
		}
	}
	//update image
	foreach($request_data as $key=>$value)
	{
		if ( strpos($key, 'ajaxf_image_') === 0 && strlen($value) > 0) {
			$img_id = substr($key, strlen('ajaxf_image_'));
			$row = $dt->get(intVal($img_id));
			if ( $row['path'] ) {
				$GLOBALS['APP_INSTANCE']->removeFile($row['path'], $GLOBALS['GV_IMAGE_PATH']);
			}
			
			$update_info = array('id'=>$img_id,'path'=>$value);
			$GLOBALS['APP_INSTANCE']->saveUploadedFile($value, $GLOBALS['GV_IMAGE_PATH'] );
			$info['path'] = $value;
			$dt->save($update_info);
		}
	}
	
	ob_clean();
	$redirect_url = 'working-photo.php?working_id='.$_REQUEST['working_id'];
	header('Location:'.$redirect_url);
	exit();
	
}
elseif (  $act == 'delete' ){
	$row = $dt->get($_REQUEST['id']);
	if ( $row ) {
		$GLOBALS['APP_INSTANCE']->removeFile($row['path'], $GLOBALS['GV_IMAGE_PATH']);
		$dt->remove($row['id']);
	}
	ob_clean();
	$redirect_url = 'working-photo.php?working_id='.$_REQUEST['working_id'];
	header('Location:'.$redirect_url);
	exit();
}

$condition = sprintf("`working_id`=%d", $_REQUEST['working_id']);
$GLOBALS['PAGE_LIST_DATA'] = $dt->get_list($condition,'id')
?>